package com.neusoft.util;


import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.DecodedJWT;
import org.tju.po.User;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class TokenUtil {

	//token 失效时间
    private static final long EXPIRE_TIME= 4*60*60*1000;
    //private static final long EXPIRE_TIME= 6*1000;
    //密钥盐
    private static final String TOKEN_SECRET="nbnbtoken";

    //签名生成
    public static Map<String,Object> sign(User user){
        String token = null;
        Date expiresAt = new Date(System.currentTimeMillis() + EXPIRE_TIME);
        try {
          //使用JWT库创建JWT对象，并设置相关的声明信息。
          // Date expiresAt = new Date(System.currentTimeMillis() + EXPIRE_TIME);
            token = JWT.create()
                    .withIssuer("auth0")
                    .withClaim("userId", user.getUserId())
                    .withExpiresAt(expiresAt)
                    .sign(Algorithm.HMAC256(TOKEN_SECRET));
        } catch (Exception e){
            e.printStackTrace();
        }
        //创建一个Map对象来存储签名信息。
        Map<String, Object> sign = new HashMap<>();
        sign.put("token", token);
        long times = expiresAt.getTime();
        //
        sign.put("expir",times);
        return sign;
    }


    //签名验证
    public static boolean verify(String token){
        try {
            JWTVerifier verifier = JWT.require(Algorithm.HMAC256(TOKEN_SECRET)).withIssuer("auth0").build();
            //使用JWTVerifier对象来验证传入的token字符串。如果验证通过，将得到一个DecodedJWT对象，其中包含了JWT的声明信息。
            DecodedJWT jwt = verifier.verify(token);
            System.out.println("认证通过：");
            System.out.println("userId: " + jwt.getClaim("userId").asString());
            System.out.println("过期时间：      " + jwt.getExpiresAt());
            return true;
        } catch (Exception e){
            throw new RuntimeException("TOKEN ERROR");
        }
    }

    public static Map<String,Object> tokenContent(String token){
        Map<String, Object> result = new HashMap<>();
        try {
            JWTVerifier verifier = JWT.require(Algorithm.HMAC256(TOKEN_SECRET)).withIssuer("auth0").build();
            DecodedJWT jwt = verifier.verify(token);
            result.put("token", token);
            long times =  jwt.getExpiresAt().getTime();
            result.put("expir",times);
            return result;
        } catch (Exception e){
        	  throw new RuntimeException("TOKEN ERROR");
        }
    }

    public static String tokenUser(String token){
        //Map<String, Object> result = new HashMap<>();
        try {
            JWTVerifier verifier = JWT.require(Algorithm.HMAC256(TOKEN_SECRET)).withIssuer("auth0").build();
            DecodedJWT jwt = verifier.verify(token);
            String userId = jwt.getClaim("userId").asString();
            return userId;
        } catch (Exception e){
        	  throw new RuntimeException("TOKEN ERROR");
        }
    }

}
